import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
var _excluded = ["_status", "id"];

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _class, _class2;

import React, { PureComponent } from 'react';
import { Form, Modal, Popconfirm, Spin } from 'antd';
import EditTable from 'components/EditTable';
import { Button as ButtonPermission } from 'components/Permission';
import { Bind } from 'lodash-decorators';
import { getEditTableData, tableScrollWidth } from 'utils/utils';
import Lov from 'components/Lov';
import intl from 'utils/intl';
import QueryForm from "./QueryForm";
var FormItem = Form.Item;

function getRefFieldsValue(ref) {
  if (ref.current) {
    return ref.current.props.form.getFieldsValue();
  }

  return {};
}

var Clients = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec11 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_PureComponent) {
  _inherits(Clients, _PureComponent);

  var _super = _createSuper(Clients);

  function Clients(props) {
    var _this;

    _classCallCheck(this, Clients);

    _this = _super.call(this, props);
    _this.form = void 0;
    _this.oneSearchFormRef = React.createRef();
    _this.state = {
      selectedRows: [] // okDisabled: false,

    };
    return _this;
  }

  _createClass(Clients, [{
    key: "handleOneSearchFormSearch",
    value:
    /**
     * 表单查询
     */
    function handleOneSearchFormSearch() {
      var onFormSearch = this.props.onFormSearch;
      var fieldsValue = getRefFieldsValue(this.oneSearchFormRef);
      onFormSearch(fieldsValue);
    }
    /**
     * 勾选回调
     * @param {*} _
     * @param {*} selectedRows - 勾选的值集行
     */

  }, {
    key: "handleSelectRows",
    value: function handleSelectRows(_, selectedRows) {
      this.setState({
        selectedRows: selectedRows
      });
    }
    /**
     * 模态框onCancel方法
     */

  }, {
    key: "handleOnCancel",
    value: function handleOnCancel() {
      var close = this.props.close;
      close();
      this.setState({
        selectedRows: []
      });
    }
    /**
     * 编辑行
     * @param {*} record
     * @param {*} flag
     */

  }, {
    key: "editRow",
    value: function editRow(record, flag) {
      var onEdit = this.props.onEdit;
      onEdit(record, flag);
    }
    /**
     * 取消编辑行
     * @param {*} record
     * @param {*} flag
     */

  }, {
    key: "handleCancelEdit",
    value: function handleCancelEdit(record, flag) {
      var onCancelEdit = this.props.onCancelEdit;
      onCancelEdit(record, flag);
    }
    /**
     * 删除客户端行
     */

  }, {
    key: "deleteRow",
    value: function deleteRow(record) {
      var onDeleteRow = this.props.onDeleteRow;
      onDeleteRow(record);
    }
    /**
     * 删除客户端
     */

  }, {
    key: "handleDeleteClient",
    value: function handleDeleteClient() {
      var _this$props = this.props,
          onDeleteClient = _this$props.onDeleteClient,
          roleDatasource = _this$props.roleDatasource;
      var selectedRows = this.state.selectedRows;
      var dataSource = selectedRows.map(function (item) {
        return {
          roleId: roleDatasource.id,
          memberId: item.id,
          memberType: 'client',
          id: item.memberRoleId
        };
      }); // 批量删除时：把新建的数据给过滤掉

      var filterList = dataSource.filter(function (item) {
        return !item.memberId.toString().startsWith('create');
      });
      onDeleteClient(filterList);
      this.setState({
        selectedRows: []
      });
    }
    /**
     * 添加客户端行
     */

  }, {
    key: "handleAddClient",
    value: function handleAddClient() {
      var onAddClient = this.props.onAddClient;
      onAddClient();
    }
    /**
     * 保存客户端
     */

  }, {
    key: "handleOnOk",
    value: function handleOnOk() {
      var _this$props2 = this.props,
          onSave = _this$props2.onSave,
          _this$props2$clientLi = _this$props2.clientList,
          clientList = _this$props2$clientLi === void 0 ? [] : _this$props2$clientLi,
          roleDatasource = _this$props2.roleDatasource;
      var params = getEditTableData(clientList, ['_status']);

      if (Array.isArray(params) && params.length > 0) {
        onSave(params.map(function (r) {
          var _status = r._status,
              id = r.id,
              restR = _objectWithoutProperties(r, _excluded);

          return _objectSpread(_objectSpread({}, restR), {}, {
            roleId: roleDatasource.id,
            assignLevel: 'organization',
            memberId: r.id,
            assignLevelValue: roleDatasource.tenantId,
            memberType: 'client'
          });
        }));
      }

      this.setState({
        selectedRows: []
      });
    }
    /**
     * 客户端分页切换
     * @param {*} pagination
     */

  }, {
    key: "onTableChange",
    value: function onTableChange() {
      var pagination = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var fieldsValue = getRefFieldsValue(this.oneSearchFormRef);
      var onClientPageChange = this.props.onClientPageChange;
      onClientPageChange(pagination, fieldsValue);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          _this$props3$clientLi = _this$props3.clientList,
          clientList = _this$props3$clientLi === void 0 ? [] : _this$props3$clientLi,
          visible = _this$props3.visible,
          title = _this$props3.title,
          path = _this$props3.path,
          roleDatasource = _this$props3.roleDatasource,
          saveLoading = _this$props3.saveLoading,
          deleteLoading = _this$props3.deleteLoading,
          queryLoading = _this$props3.queryLoading,
          clientPagination = _this$props3.clientPagination;
      var roleId = roleDatasource.id;
      var selectedRows = this.state.selectedRows; // const params = getEditTableData(clientList, ['id', '_status']);

      var rowSelection = {
        selectedRowKeys: selectedRows.map(function (n) {
          return n.id;
        }),
        onChange: this.handleSelectRows
      };
      var columns = [{
        title: intl.get('ciam.roleManagement.model.roleManagement.clientName').d('客户端名称'),
        width: 300,
        dataIndex: 'name',
        render: function render(id, record) {
          if (record._status === 'create') {
            var _record$$form = record.$form,
                getFieldDecorator = _record$$form.getFieldDecorator,
                setFieldsValue = _record$$form.setFieldsValue;

            var onClientChange = function onClientChange(_, client) {
              setFieldsValue({
                id: client.id,
                tenantName: client.tenantName
              });
            };

            return /*#__PURE__*/React.createElement(FormItem, null, getFieldDecorator('id', {
              initialValue: record.name,
              rules: [{
                required: true,
                message: intl.get('cpaas.common.validation.notNull', {
                  name: intl.get('ciam.roleManagement.model.roleManagement.clientName').d('客户端名称')
                })
              }]
            })( /*#__PURE__*/React.createElement(Lov, {
              queryParams: {
                roleId: roleId
              },
              code: "CIAM.CLIENT",
              onChange: onClientChange,
              textValue: id
            })));
          } else {
            return id;
          }
        }
      }, {
        title: intl.get("ciam.roleManagement.model.roleManagement.tenant").d('所属租户'),
        dataIndex: 'tenantName',
        width: 200,
        render: function render(tenantName, record) {
          if (record._status === 'create') {
            var _record$$form2 = record.$form,
                getFieldValue = _record$$form2.getFieldValue,
                getFieldDecorator = _record$$form2.getFieldDecorator;
            return /*#__PURE__*/React.createElement(React.Fragment, null, getFieldValue('tenantName'), getFieldDecorator('tenantName')( /*#__PURE__*/React.createElement("div", null)));
          } else {
            return tenantName;
          }
        }
      }];
      return /*#__PURE__*/React.createElement(Modal, {
        title: title,
        visible: visible,
        destroyOnClose: true,
        onCancel: this.handleOnCancel,
        confirmLoading: saveLoading,
        wrapClassName: "ant-modal-sidebar-right",
        transitionName: "move-right",
        onOk: this.handleOnOk // okButtonProps={{disabled: !okDisabled}}
        ,
        width: "700px"
      }, /*#__PURE__*/React.createElement(QueryForm, {
        wrappedComponentRef: this.oneSearchFormRef,
        onSearch: this.handleOneSearchFormSearch,
        roleDatasource: roleDatasource
      }), /*#__PURE__*/React.createElement("br", null), /*#__PURE__*/React.createElement("div", {
        className: "action",
        style: {
          textAlign: 'right'
        }
      }, /*#__PURE__*/React.createElement(Popconfirm, {
        title: intl.get('cpaas.common.message.confirm.remove').d('确定删除选中数据？'),
        onConfirm: this.handleDeleteClient
      }, /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.deleteClients"),
          type: 'button',
          meaning: '角色管理-删除客户端'
        }],
        disabled: selectedRows.length === 0,
        style: {
          marginRight: 8
        },
        loading: deleteLoading
      }, intl.get('cpaas.common.button.delete').d('删除'))), /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.addClients"),
          type: 'button',
          meaning: '角色管理-新建客户端'
        }],
        type: "primary",
        onClick: this.handleAddClient
      }, intl.get("cpaas.common.button.create").d('新建'))), /*#__PURE__*/React.createElement("br", null), /*#__PURE__*/React.createElement(Spin, {
        spinning: queryLoading
      }, /*#__PURE__*/React.createElement(EditTable, {
        rowKey: "id",
        dataSource: clientList,
        bordered: true,
        className: "editable-table",
        columns: columns,
        onChange: this.onTableChange,
        onRow: this.onTableRow,
        rowSelection: rowSelection,
        scroll: {
          x: tableScrollWidth(columns)
        },
        pagination: clientPagination
      })));
    }
  }]);

  return Clients;
}(PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "handleOneSearchFormSearch", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handleOneSearchFormSearch"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSelectRows", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSelectRows"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleOnCancel", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleOnCancel"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "editRow", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "editRow"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCancelEdit", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCancelEdit"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "deleteRow", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "deleteRow"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDeleteClient", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDeleteClient"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleAddClient", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "handleAddClient"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleOnOk", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "handleOnOk"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onTableChange", [_dec11], Object.getOwnPropertyDescriptor(_class2.prototype, "onTableChange"), _class2.prototype)), _class2)) || _class);
export { Clients as default };